/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PowerPointApp.java
 *
 * Created on Dec 8, 2012, 6:01:51 PM
 */
package UserInterface;

import GestureRecognition.Slider;
import controller.ColorMeanController;
import controller.SkinBasedController;
import coordinates.ArrayFilter;
import insurface.InteractiveSurface;
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import setting.InitialSettings;

/**
 *
 * @author HOME
 */
public class PowerPointApp extends InteractiveSurface implements Runnable {

    public int lcam;
    public int rcam;
    File f;
    private boolean isFingerSet = true;
    JFileChooser fchose = null;
    private setting.InitialSettings is;
    Dimension dim = null;
    Toolkit toolkit = null;

    public InitialSettings getIs() {
        return is;
    }

    public void setIs(InitialSettings is) {
        this.is = is;
    }

    /** Creates new form PowerPointApp */
    public PowerPointApp() {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        openButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Power Point Selector");

        openButton.setFont(new java.awt.Font("Arial", 1, 12));
        openButton.setText("Open File");
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 24));
        jLabel1.setText("Power Point Viewer");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/powerpoint_small.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(openButton)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(openButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
        openButton.setEnabled(false);
        toolkit = Toolkit.getDefaultToolkit();
        dim = toolkit.getScreenSize();
        Desktop dt = Desktop.getDesktop();

        fchose = new JFileChooser();
        int returnVal = fchose.showDialog(this, "Open");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            f = fchose.getSelectedFile();
            try {

                dt.open(f);

            } catch (IOException ex) {
                Logger.getLogger(PowerPointApp.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (isFingerSet) {
            SkinBasedController sbc = new SkinBasedController(lcam, rcam, this);
            control = sbc;
            Thread th = new Thread(sbc);
            th.start();
        } else {
            ColorMeanController cmc = new ColorMeanController(lcam, rcam, this);
            control = cmc;
            Thread th = new Thread(cmc);
            th.start();
        }

        //    System.out.println("width="+dim.width+" height="+dim.height);
        System.out.println(dim.width);
        new Thread(this).start();

    }//GEN-LAST:event_openButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } catch (Exception e) {
                    System.out.println("Look and Feel doesn't available!!!");
                }
                new PowerPointApp().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton openButton;
    // End of variables declaration//GEN-END:variables

    public void setLcam(int lcam) {
        this.lcam = lcam;
    }

    public void setRcam(int rcam) {
        this.rcam = rcam;
    }

    public void setIsFingerSet(boolean isFingerSet) {
        this.isFingerSet = isFingerSet;
    }

    Robot rb = null;
    public void run() {
        toolkit = Toolkit.getDefaultToolkit();
        dim = toolkit.getScreenSize();



        Slider sl = new Slider();
        int[] cordinate = new int[2];
        ArrayFilter af = new ArrayFilter(5);
        try {
            rb = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(PowerPointApp.class.getName()).log(Level.SEVERE, null, ex);
        }

        while (control.camActive) {
            try {
                //System.out.println("while");
                Thread.sleep(80);
            } catch (InterruptedException ex) {
                Logger.getLogger(PowerPointApp.class.getName()).log(Level.SEVERE, null, ex);
            }
            cordinate[0] = dim.width - getX1();
            cordinate[1] = getY1();
            //cordinate = af.getMedian(cordinate[0], cordinate[1]);

            if ((cordinate[0] == dim.width + 10) && (cordinate[1] == -10)) {
                //System.out.println("awa" + cordinate[0] + "," + cordinate[1]);
                continue;
            } else {
                cordinate = af.movingAverage(cordinate[0], cordinate[1]);
                inRange(cordinate);

                if (!redPressed) {
                    //System.out.println("x= " + cordinate[0] + "y= " + cordinate[1]);
                    sl.identifyGestureType(cordinate);
                    rb.mouseMove(cordinate[0], cordinate[1]);
                } else {      
                    rb.mouseRelease(InputEvent.BUTTON1_MASK);
                    rb.mousePress(InputEvent.BUTTON1_MASK);
                    rb.mouseMove(cordinate[0], cordinate[1]);
                }


//
            }
        }
    }
    public int count = 0;
    public boolean redPressed=false;

    public  void inRange(int[] cordinate) {

        if ((cordinate[0] >= 0 && cordinate[0] < 300) && (cordinate[1] < dim.height && cordinate[1] > dim.height - 300)) {
            count++;
            if(count==40){
                count=0;        
                redPressed=!redPressed;
                if(redPressed){penkeyPressing(rb);}
                else{arrKeyPreessing(rb);}
            }
        }
        else{
            count=0;
        }

    }

    public void penkeyPressing(Robot rb){
        System.out.println("ctrl");
        rb.keyPress(KeyEvent.VK_CONTROL);  
        rb.keyPress(KeyEvent.VK_P);
        rb.keyRelease(KeyEvent.VK_CONTROL);
        rb.keyRelease(KeyEvent.VK_P);
//        rb.mousePress(InputEvent.BUTTON1_MASK);
//        rb.mouseRelease(InputEvent.BUTTON1_MASK);
    }
    public void arrKeyPreessing(Robot rb){
        System.out.println("arrow");
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_A);
        rb.keyRelease(KeyEvent.VK_CONTROL);
        rb.keyRelease(KeyEvent.VK_A);

    }



}
